package com.jingan.nursinghome.regulatory.adapter;

import android.graphics.Color;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.jingan.nursinghome.regulatory.R;
import com.jingan.nursinghome.regulatory.data.MicrometerTable;
import com.jingan.nursinghome.regulatory.dialog.ScoreDialog;
import com.jingan.nursinghome.regulatory.dialog.ZhengaiDialog;
import com.jingan.nursinghome.regulatory.view.KongView;
import com.jingan.nursinghome.regulatory.view.RectificationView;
import com.jingan.nursinghome.regulatory.view.SpannerView;
import com.jingan.nursinghome.regulatory.view.iView;
import com.jingan.nursinghome.regulatory.view.triangleView;

import cn.android.support.v7.lib.sin.crown.base.BaseActivity;
import cn.android.support.v7.lib.sin.crown.base.BaseListenner;
import cn.android.support.v7.lib.sin.crown.utils.ProportionUtils;

/**
 * Created by 彭治铭 on 2017/10/12.
 */

public class MicrometerTableAdapter3 extends RecyclerView.Adapter<MicrometerTableAdapter3.MyViewHolder> {
    BaseActivity baseActivity;

    public MicrometerTableAdapter3(BaseActivity baseActivity) {
        this.baseActivity = baseActivity;
    }

    //ViewHolder和View绑定在一起，构造函数一般都是初始化控件
    public class MyViewHolder extends RecyclerView.ViewHolder {
        iView txt_question;
        RectificationView rectifi;//整改
        EditText edit_qt;

        int position = 0;

        public MyViewHolder(View itemView) {
            super(itemView);
            if (MicrometerTable.getInstance().topicsZX.size() == 0) {
                return;
            }
            txt_question = (iView) itemView.findViewById(R.id.txt_question);
            txt_question.setmWidth(ProportionUtils.getInstance().adapterInt(530));
            rectifi = (RectificationView) itemView.findViewById(R.id.rectifi);
            rectifi.setWidhtAndHeiht(ProportionUtils.getInstance().adapterInt(118), ProportionUtils.getInstance().adapterInt(66));
            edit_qt = (EditText) itemView.findViewById(R.id.edit_qt2);
            ProportionUtils.getInstance().adapterTextView(edit_qt);
        }
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (MicrometerTable.getInstance().topicsZX.size() == 0) {
            return new MyViewHolder(new KongView(parent.getContext()));
        }
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_micrometertablleos_item3, parent, false);//这个是正确的加载方式。参数3必须为false，否则报错。
        return new MyViewHolder(view);
    }


    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        if (MicrometerTable.getInstance().topicsZX.size() <= 0) {
            return;
        }
        String i = MicrometerTable.getInstance().topicsZX.get(position).getCount();//题号
        //Log.e("test","题号:\t"+i);
        holder.txt_question.setInfo(i, MicrometerTable.getInstance().topicsZX.get(position).getQuestion());//题目
        if (MicrometerTable.getInstance().topicsZX.get(position).getZhengai().equals("0")) {
            holder.rectifi.setInfoAndColor("整改", Color.parseColor("#666666"));
        }
        if (MicrometerTable.getInstance().topicsZX.get(position).getZhengai().equals("1")) {
            holder.rectifi.setInfoAndColor("现场整改", Color.parseColor("#4B97F0"));
        }
        if (MicrometerTable.getInstance().topicsZX.get(position).getZhengai().equals("2")) {
            holder.rectifi.setInfoAndColor("计划整改", Color.parseColor("#4B97F0"));
        }


        //防止Eidtor在适配器中错乱。在holder里自主添加postion属性判断。
        holder.position = position;
        holder.edit_qt.setText(MicrometerTable.getInstance().topicsZX.get(position).getRemark());
        holder.edit_qt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                //为了防止eidtor适配器错乱。必须在holder里面添加一个position属性进行标示。防止错乱。以下判断亲测OK
                if (holder.edit_qt.isFocused() && position == holder.position) {
                    MicrometerTable.getInstance().topicsZX.get(position).setRemark(s.toString());//s.toString()就是文本
                }
            }
        });

        holder.rectifi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ZhengaiDialog.getInstance(baseActivity, Integer.valueOf(MicrometerTable.getInstance().topicsZX.get(position).getZhengai()), new BaseListenner<Integer>() {
                    @Override
                    public void onResult(Integer result) {
                        MicrometerTable.getInstance().topicsZX.get(position).setZhengai(result + "");
                        if (MicrometerTable.getInstance().topicsZX.get(position).getZhengai().equals("0")) {
                            holder.rectifi.setInfoAndColor("整改", Color.parseColor("#666666"));
                        }
                        if (MicrometerTable.getInstance().topicsZX.get(position).getZhengai().equals("1")) {
                            holder.rectifi.setInfoAndColor("现场整改", Color.parseColor("#4B97F0"));
                        }
                        if (MicrometerTable.getInstance().topicsZX.get(position).getZhengai().equals("2")) {
                            holder.rectifi.setInfoAndColor("计划整改", Color.parseColor("#4B97F0"));
                        }
                    }
                });
            }
        });

        //高度調整
        holder.itemView.findViewById(R.id.dialog_micrometertableos_constraintLayout).getLayoutParams().height = ProportionUtils.getInstance().adapterInt(439) + (holder.txt_question.LineCount - 1) * ProportionUtils.getInstance().adapterInt(40);
        holder.itemView.findViewById(R.id.dialog_micrometertableos_constraintLayout).getLayoutParams().width = (int) ProportionUtils.getInstance().realWidthPixels;
        holder.itemView.findViewById(R.id.dialog_micrometertableos_constraintLayout).requestLayout();

        //Log.e("test", "题号:\t" + i + "\t行数:\t" + holder.txt_question.LineCount);
    }

    @Override
    public int getItemCount() {
        int count = MicrometerTable.getInstance().topicsZX.size();
        if (count == 0) {
            return 1;
        }
        return MicrometerTable.getInstance().topicsZX.size();
    }
}
